<?php
class Controllerstockfactory extends Controller {

	//仓库初始化
	public function index() {

		$this->load->model('stock/factory');

		$this->document->setTitle('工厂对账单');
		

		$this->getList();

    }

	private function getList(){

		$page = I('get.page');
	    if(!$page){
	      $page = 1;
	    }
	    $data = I('get.');

	    if(!empty($data['factory'])){
	      $factory = trim($data['factory']);
	      $filter['money_out.payee_id'] = array('eq',$factory);
	    }
		if (!empty($data['filter_date_end'])) {
	        $filter_date_end = $data['filter_date_end'];
	        $filter['money_out.date_added'] = array('lt', $filter_date_end.' 23:59:59');
	    }

	     if(!empty($data['is_zero'])){
	      $is_zero = trim($data['is_zero']);
	    }else{
	      $data['is_zero'] = '*';
	    }
	     $acount =  $this->model_stock_factory->getList($page,$filter);
	     foreach ($acount as $key => $value) { 
	     	$acount[$key]['differ'] =($value['payables']-$value['paid']); 
	     	$acount[$key]['podiffer'] =($value['refer_total']-$value['payables']); 
	     	if ($is_zero==1&&$acount[$key]['differ']!=0) {
	     		unset($acount[$key]);
	     	}
	     	if ($is_zero==2&&$acount[$key]['differ']==0) {
	     		unset($acount[$key]);
	     	}
	     }
	    $data['acount'] = $acount;
	  	$order_total =count($this->model_stock_factory->getCount($filter));
	    $pagination = new Pagination();
	    $pagination->total = $order_total;
	    $pagination->page = $page;
	    $pagination->limit = $this->config->get('config_limit_admin');
	    $pagination->url = $this->url->link('stock/factory', 'token=' . $this->session->data['token'] . '&page={page}'.'&filter_date_end='.$filter_date_end.'&filter_date_start='.$filter_date_start.'&factory='.$factory.'&is_zero='.$is_zero, 'SSL');

	    $data['pagination'] = $pagination->render();
	    $data['stocl_list'] = $stocl_list;
		$data['results'] = sprintf($this->language->get('text_pagination'), ($order_total) ? (($page - 1) * $this->config->get('config_limit_admin')) + 1 : 0, ((($page - 1) * $this->config->get('config_limit_admin')) > ($order_total - $this->config->get('config_limit_admin'))) ? $order_total : ((($page - 1) * $this->config->get('config_limit_admin')) + $this->config->get('config_limit_admin')), $order_total, ceil($order_total / $this->config->get('config_limit_admin')));

		/*面包屑导航栏*/
		$data['heading_title'] = '工厂对账单';
		$data['breadcrumbs'][] = array(
			'text' => '首页',
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);
		$data['breadcrumbs'][] = array(
			'text' =>'工厂对账单',
			'href' => $this->url->link('stock/money_in', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);
		/*面包屑导航栏*/
		
		$data['text_list'] = '工厂对账单';


		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');
		$this->response->setOutput($this->load->view('stock/factory.tpl', $data));

	}


	public function detail(){
		$this->load->model('stock/factory');

		$this->document->setTitle('工厂对账单');
		$this->document->addScript('view/javascript/angular.js');
    	$this->document->addScript('view/javascript/bhz_ctl.js');
		$data = I('get.');
		$payee_id = $data['payee_id'];
		$money_out_id = $data['money_out_id'];
	    $data['money_out'] =  $this->model_stock_factory->getMoneyOut($payee_id);
	    $total = 0;
	    foreach ($data['money_out'] as $key => $value) {
	    	$total+=$value['payables'];
	    }
	     $data['total'] = $total;

		/*面包屑导航栏*/
		$data['heading_title'] = '工厂对账单';
		$data['breadcrumbs'][] = array(
			'text' => '首页',
			'href' => $this->url->link('common/dashboard', 'token=' . $this->session->data['token'], 'SSL')
		);
		$data['breadcrumbs'][] = array(
			'text' =>'工厂对账单',
			'href' => $this->url->link('stock/money_in', 'token=' . $this->session->data['token'] . $url, 'SSL')
		);
		/*面包屑导航栏*/
		
		$data['text_list'] = '工厂对账单';


		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');
		$this->response->setOutput($this->load->view('stock/factory_view.tpl', $data));
	}
	

	public function factoryAC(){
		$json = array();

		if (isset($this->request->get['filter_name'])) {
			$filter_name = trim($this->request->get['filter_name']);
		}
		else{
			$filter_name = '';
		}

		$this->load->model('stock/factory');
		

		$results = $this->model_stock_factory->getAutoCompletefactory($filter_name);

		foreach ($results as $result){
			$json[] = array(
				'vendor_name' => $result['vendor_name'],
				'user_id' => $result['user_id']
				

			);
		}

		$this->response->addHeader('Content-Type: application/json');
		$this->response->setOutput(json_encode($json));
	}

}